
var url = "http://sports.sina.com.cn/nba/1.shtml";//声明数据源地址
var http = require("http");
var cheerio = require("cheerio");
var fs = require("fs");

getData(url,function($){
	$("#right a").each(function(index){
		var docUrl = $(this).attr("href");//便利a标签，获取href属性
		getData(docUrl,function($){
			var article = $("#artibody").text();
			fs.writeFileSync("./news/" + new Date().valueOf() + ".text", article)
		})
	})
})



function getData(url,callback) {
	http.get(url,function(res){
		var html = "";
		res.on("data",function(chunk){
			html += chunk;//拼接数据段
		});
		res.on("end",function(){
			var $ = cheerio.load(html);
			callback($);
		});
	}).on("error",function(err){
		console.log(err.message)
	})
}